<?php
/**
 * Description:
 *
 * @author      Eden
 * @datetime    2016/11/26 16:39
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */

/**
 * 获取客户端IP
 * @return string
 */
function remoteAddr()
{
    $ip = '';
    if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    } else if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } else if (!empty($_SERVER['HTTP_X_FORWARDED'])) {
        $ip = $_SERVER['HTTP_X_FORWARDED'];
    } else if (!empty($_SERVER['HTTP_X_CLUSTER_CLIENT_IP'])) {
        $ip = $_SERVER['HTTP_X_CLUSTER_CLIENT_IP'];
    } else if (!empty($_SERVER['HTTP_FORWARDED_FOR'])) {
        $ip = $_SERVER['HTTP_FORWARDED_FOR'];
    } else if (!empty($_SERVER['HTTP_FORWARDED'])) {
        $ip = $_SERVER['HTTP_FORWARDED'];
    } else if (!empty($_SERVER['REMOTE_ADDR'])) {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}

/**
 * 无报错取值
 *
 * @param $params
 * @param $field
 * @param null $default
 * @return array|null
 */
function value($params, $field, $default = null)
{
    if (is_array($params)) {
        if (is_string($field)) {
            return isset($params[$field]) ? $params[$field] : $default;
        }

        if (is_array($field)) {
            $tmpVal = $params;
            foreach($field as $key) {
                if (isset($tmpVal[$key])) {
                    $tmpVal = $tmpVal[$key];
                } else {
                    return NULL;
                }
            }
            return $tmpVal;
        }
    }

    if (is_string($params)) {
        return $params;
    }

    if (is_object($params)) {
        return isset($params->$field) ? $params->$field : $default;
    }

    return $params;
}

/**
 * 检查或创建文件上传路径
 * @param $type
 * @return bool|string
 */
function checkUpPath($type = '')
{
    $state = true;
    $ci = &get_instance();
    $uploadDir = $ci->config->item('upload_dir');
    $savePath = rtrim($uploadDir, '/') . '/' . strtolower($type);
    if(!file_exists($savePath) || !is_dir($savePath)) {
        $state = @mkdir($savePath, 0777, true);
    } else {
        if (is_dir($savePath) && !is_writable($savePath)) {
            $state = @chmod($savePath, 0777);
        }
    }
    return $state ? $savePath : false;
}

/**
 * 密码加密算法
 * @param $password
 * @return array
 */
function encryptPassword($password)
{
    $salt = substr(uniqid(), 0, 6);
    return array( 'password' => md5($password . $salt), 'salt' => $salt);
}

/**
 * 密码验证算法
 * @param $org
 * @param $enc
 * @param $salt
 * @return bool
 */
function decryptPassword($org, $enc, $salt)
{
    return $enc === md5($org . $salt);
}

/**
 * 当时时间
 * @return bool|string
 */
function dateTime()
{
    return date('Y-m-d H:i:s');
}

/**
 * 生成订单号
 * @return string
 */
function sn()
{
    $yCode = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J');
    $orderSn = $yCode[intval(date('Y')) - 2016] . strtoupper(dechex(date('m'))) . date('d') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));
    return $orderSn;
}

/**
 * 检查手机号
 * @param $phone
 * @return bool
 */
function checkPhone($phone)
{
    return preg_match('/^1[34578]\d{9}$/', $phone) ? true : false;
}

/**
 * 获取管理员ID
 * @return array|int|null
 */
function adminId()
{
    $ci = &get_instance();
    if ($ci->session->has_userdata('adminDetail')) {
        $detail = $ci->session->adminDetail;
        return value($detail, 'adminId', 0);
    }
    return 0;
}

/**
 * 获取管理员详情
 * @return array
 */
function adminDetail()
{
    $ci = &get_instance();
    $detail = array();
    if ($ci->session->has_userdata('adminDetail')) {
        $detail = $ci->session->adminDetail;
    }
    return $detail;
}

/**
 * 创建代金券
 * @return string
 */
function createVoucher()
{
    return strtoupper(uniqid());
}

/**
 * 用户增加
 * @param string $source
 */
function statIncr($field, $incr = 1)
{
    $ci = &get_instance();
    $date = date('Y-m-d');
    $anaDb = clone($ci->db);
    $query = $anaDb->get_where('analytics_daily', array('date' => $date));
    $row = $query->row_array();
    if ($row) {
        // 改
        if (is_array($field)) foreach($field as $key => $value) {
            $anaDb->set($key, $key . "+{$value}", false);
        } else {
            $anaDb->set($field, $field . "+{$incr}", false);
        }
        $anaDb->where('date', $date);
        $anaDb->update('analytics_daily');
    } else {
        // 增
        $dailyData['date'] = $date;
        if (is_array($field)) foreach($field as $key => $value) {
            $dailyData[$key] = $value;
        } else {
            $dailyData[$field] = $incr;
        }
        $anaDb->insert('analytics_daily', $dailyData);
    }
}